package pl.softech.oldchart;

import java.awt.Color;
import java.awt.Cursor;
import java.awt.GridLayout;
import java.awt.Point;
import java.awt.Rectangle;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.util.Collection;
import java.util.HashMap;
import java.util.Map;

import javax.swing.BorderFactory;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.border.Border;

public class GraphLegendFactory {

	public interface IGraphLegend {
		public void addModel(IGraph2DModel model);
		public void removeModel(IGraph2DModel model);
	}
 	
	protected class MouseLegendModelAdapter extends MouseAdapter {

		private Point beginPoint;
		private JPanel panel;
		private Rectangle bounds;
		
		public MouseLegendModelAdapter(JPanel panel) {
			this.panel = panel;
		}
		
		@Override
		public void mouseExited(MouseEvent e) {
			beginPoint = null;
			panel.setCursor(Cursor.getPredefinedCursor(Cursor.DEFAULT_CURSOR));
		}
		
		@Override
		public void mouseMoved(MouseEvent e) {
			if(beginPoint == null) return;
			Point endPoint = e.getLocationOnScreen();
			Point distance = new Point(endPoint.x - beginPoint.x, endPoint.y - beginPoint.y);
			Rectangle bounds = (Rectangle) this.bounds.clone();
			bounds.x += distance.x;
			bounds.y += distance.y;
			panel.setBounds(bounds);
		}
		
		@Override
		public void mousePressed(MouseEvent e) {
			if(e.getButton() == MouseEvent.BUTTON1) {
				if(beginPoint != null) {
					beginPoint = null;
					panel.setCursor(Cursor.getPredefinedCursor(Cursor.DEFAULT_CURSOR));
					return;
				}
				panel.setCursor(Cursor.getPredefinedCursor(Cursor.HAND_CURSOR));
				beginPoint = e.getLocationOnScreen();
				bounds = panel.getBounds();
			}
		}
		
	}
	
	private Collection<IGraph2DModel> models;
	
	public GraphLegendFactory(Collection<IGraph2DModel> models) {
		
		this.models = models;
		
	}
	
	private class SimpleGraphLegend extends JPanel implements IGraphLegend {
		
		private static final long serialVersionUID = 1L;
		private MouseAdapter labelMouseAdapter;
		
		private Map<JLabel, IGraph2DModel> labelToModel;
		
		public SimpleGraphLegend() {
			super(new GridLayout(models.size(), 1));
			init();
		} 
		
		private void initLabelMouseAdapter() {
			labelMouseAdapter = new MouseAdapter() {
				private Color foregroundColor;
				boolean drawModelCoordinates;
				@Override
				public void mouseEntered(MouseEvent e) {
					JLabel l = (JLabel) e.getSource();
					foregroundColor = l.getForeground();
					Color color = (foregroundColor.equals(Color.BLACK) ? Color.PINK : Color.BLACK);
					labelToModel.get(l).setGraphColor(color);
					drawModelCoordinates = labelToModel.get(l).drawCoordinates();
					labelToModel.get(l).setDrawCoordinates(true);
					labelToModel.get(l).setSelected(true);
					l.setForeground(color);labelToModel.get(l);
					getRootPane().repaint();
					
				}
				
				@Override
				public void mouseExited(MouseEvent e) {
					JLabel l = (JLabel) e.getSource();
					labelToModel.get(l).setGraphColor(foregroundColor);
					l.setForeground(foregroundColor);
					labelToModel.get(l).setDrawCoordinates(drawModelCoordinates);
					labelToModel.get(l).setSelected(false);
					getRootPane().repaint();
				}
				
			};
		}
		
		private void init() {
			int inset = 5;
			Border outsideBorder = BorderFactory.createMatteBorder(4*inset, 0, 0, 0, Color.BLACK);
			Border insideBorder = BorderFactory.createCompoundBorder(
					BorderFactory.createRaisedBevelBorder(),
					BorderFactory.createEmptyBorder(inset, inset, inset, inset)
				);
			setBorder(BorderFactory.createCompoundBorder(outsideBorder, insideBorder));
			
			initLabelMouseAdapter();
			labelToModel = new HashMap<JLabel, IGraph2DModel>();
			for(IGraph2DModel model : models) {
				addModel(model);
			}
			
			MouseLegendModelAdapter ml = new MouseLegendModelAdapter(this);
			addMouseListener(ml);
			addMouseMotionListener(ml);
			
		}
		
		public void removeModel(IGraph2DModel model) {
			for(JLabel l : labelToModel.keySet())
				if(labelToModel.get(l).equals(model)) {
					remove(l);
					break;
				}
		}
		
		public void addModel(IGraph2DModel model) {
			JLabel l = new JLabel(model.getName());
			labelToModel.put(l, model);
			l.addMouseListener(labelMouseAdapter);
			l.setOpaque(true);
			l.setForeground(model.getGraphColor());
			add(l);
		}
		
		
		
	}
	
	public JPanel createSimpleGraphLegend() { return new SimpleGraphLegend(); }
	
}
